﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Entity;
namespace CafeTek
{
    public partial class FrmListProductType : Form
    {
        public ProductType GProductType = null; //when choose productType on treeview
        public int ProductTypeIdChoose = -1; //then choose a productType on listview
        public FrmListProductType()
        {
            InitializeComponent();
        }

        public ProductDB gproductdb = new ProductDB();
        public ProductTypeDB gproductTypeDb;

       /// <summary>
       /// add node to productType
       /// </summary>
       /// <param name="Node"></param>
       /// <param name="item"></param>
        public void AddToTreeView(TreeNode Node, ProductType item)
        {
            IList<ProductType> _listProductType = new List<ProductType>();
            ProductTypeDB _productTypeDb = new ProductTypeDB();

            _listProductType = _productTypeDb.GetAll(item);

            Node.Nodes.Clear();
            foreach (ProductType _item in _listProductType)
            {
                TreeNode _node = new TreeNode(_item.Name);
                if (_item.Visible == false)
                    _node.ForeColor = Color.Green;
                _node.Tag = _item;
                Node.Nodes.Add(_node);
                //AddToTreeView(_node, _item);
            }
        }

       /// <summary>
       /// Load product Type on treeview
       /// </summary>
        public void LoadTreeView()
        {
            IList<ProductType> _listProductType = new List<ProductType>();
            ProductTypeDB _productTypeDb = new ProductTypeDB();

            _listProductType = _productTypeDb.GetAll(null);

            foreach (ProductType item in _listProductType)
            {
                TreeNode node = new TreeNode(item.Name);
                node.Tag = item;
                AddToTreeView(node, item);
                treevProducttype.Nodes.Add(node);
            }
        }

        /// <summary>
        /// when choose product from treeview then load data on listview
        /// </summary>
        /// <param name="itemselect">when click at treeview</param>
        public void LoadProductToList(ProductType itemselect)
        {
            IList<ProductType> list = new List<ProductType>();
            gproductTypeDb = new ProductTypeDB();
            GProductType = new ProductType();
            GProductType = itemselect;
            int autonumber = 0;
            lstvProductType.Items.Clear();

            //get all product of productType
            list = gproductTypeDb.GetAll(itemselect);

            foreach (ProductType item in list)
            {
                ListViewItem itemlistview = new ListViewItem((++autonumber).ToString());
                itemlistview.SubItems.Add(item.Name);
                itemlistview.SubItems.Add(item.Description);

                itemlistview.Tag = item;

                if (!item.Visible)
                    itemlistview.SubItems.Add("");
                else
                    itemlistview.SubItems.Add("X");

                lstvProductType.Items.Add(itemlistview);
            }
        }

        private void FrmProductTypeManager_Load(object sender, EventArgs e)
        {
            LoadTreeView();
        }

        private void lstvProductType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstvProductType.SelectedItems.Count == 0)
                return;
            ProductType _produtTypeTemp = new ProductType();
            _produtTypeTemp = (ProductType)lstvProductType.SelectedItems[0].Tag;
            ProductTypeIdChoose = _produtTypeTemp.TypeId;
            txtname.Text = _produtTypeTemp.Name;
            txtDescription.Text = _produtTypeTemp.Description;
            chkVisible.Checked = _produtTypeTemp.Visible;

        }

        private void treevProducttype_AfterSelect_1(object sender, TreeViewEventArgs e)
        {
            LoadProductToList((ProductType)(treevProducttype.SelectedNode.Tag));
        }

        /// <summary>
        /// Check name and description input true
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public string checkvalue(string name, string description)
        {
            if (name.Trim() == "")
                return "Please input product type name";
            if (description.Trim() == "")
                return "Please input product type description";
            return "";

        }

        public void resetTextbox()
        {
            txtDescription.Text = "";
            txtname.Text = "";
        }

        /// <summary>
        /// Create a productType
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="visible"></param>
        /// <param name="parent"></param>
        /// <returns>ProductType</returns>
        public ProductType createproductType(string name, string description, bool visible, ProductType parent)
        {
            ProductType productType = new ProductType();
            productType.Name = name;
            productType.Description = description;
            productType.Visible = visible;
            productType.ParentType = parent;
            return productType;
        }

        /// <summary>
        /// Insert a product type
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btnadd_Click(object sender, EventArgs e)
        {
            ProductTypeDB productTypeDb = null;
            ProductType productTypeTemp = null;
            string name = txtname.Text.Trim();
            string description = txtDescription.Text.Trim();
            string message = checkvalue(name, description);

            if (message != "")
            {
                MessageBox.Show(message);
                return;
            }

            bool visible = chkVisible.Checked;
            productTypeTemp = new ProductType();
            productTypeDb = new ProductTypeDB();

            //create a productType            
            productTypeTemp = createproductType(name, description, visible, GProductType);

            //check productType have exist
            if (productTypeDb.CountProductType(productTypeTemp) <= 0)
            {
                gproductTypeDb.InsertProductType(productTypeTemp);

                //refresh treeview
                LoadProductToList((ProductType)(treevProducttype.SelectedNode.Tag));
                AddToTreeView(treevProducttype.SelectedNode, GProductType);
                resetTextbox();
            }
        }

        /// <summary>
        /// Modify a productType
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ProductTypeIdChoose == -1)
            {
                MessageBox.Show("Please choose one product type in list to update");
                return;
            }
            string name = txtname.Text.Trim();
            string description = txtDescription.Text.Trim();
            bool visible = chkVisible.Checked;
            string message = checkvalue(name, description);

            if (message != "")
            {
                MessageBox.Show(message);
                return;
            }

            ProductType productTypeTemp = new ProductType();
            //create a productType
            productTypeTemp = createproductType(name, description, visible, GProductType);

            productTypeTemp.TypeId = ProductTypeIdChoose;
            gproductTypeDb.UpdateProductType(productTypeTemp);

            //refresh listview
            LoadProductToList((ProductType)(treevProducttype.SelectedNode.Tag));
            //refresh treeview
            AddToTreeView(treevProducttype.SelectedNode, GProductType);

            ProductTypeIdChoose = -1;
            resetTextbox();
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            //not choose product type to delete
            if (ProductTypeIdChoose == -1)
            {
                MessageBox.Show("Please choose one product type in list to DELECT");
                return;
            }

            ProductType productType = new ProductType();
            productType.TypeId = ProductTypeIdChoose;

            //producttype have product
            if (gproductTypeDb.checkProduct(productType))
            {
                MessageBox.Show("This product Type have product. You must delete it before");
                return;
            }

            if (gproductTypeDb.CheckHaveChild(productType))
            {
                MessageBox.Show("This product Type have product. You must delete it before");
                return;
            }
            gproductTypeDb.DeleteProductType(productType);

            LoadProductToList((ProductType)(treevProducttype.SelectedNode.Tag));
            AddToTreeView(treevProducttype.SelectedNode, GProductType);
            ProductTypeIdChoose = -1;
        }

        private void lblAdd_Click(object sender, EventArgs e)
        {
            frmProductType f = new frmProductType();
            f.ShowDialog();
        }
    }
}
